package ansi.servlet;

import java.io.IOException;
import java.util.Collection;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import ansi.modelo.Modulo;
import ansi.modelo.Residente;
import ansi.modelo.Usuario;
import ansi.negocio.GestionModulo;
import ansi.negocio.GestionResidente;

/**
 * Servlet implementation class PortadaServlet
 */
@WebServlet("/PortadaServlet")
public class PortadaServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public PortadaServlet() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.processRequest(request, response);
	}

	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		Usuario usuario_actual = (Usuario)session.getAttribute("USUARIO_ACTUAL");
		if (usuario_actual == null){
			request.getRequestDispatcher("error.jsp").forward(request, response);
			return;
		}
		try {
			GestionModulo gestionModulo	= new GestionModulo();
			Collection<Modulo> lista			= gestionModulo.listar(usuario_actual.getRol_idRol());
			session.setAttribute("MENU_OPCIONES", lista);
		} catch(Exception ex) {
			ex.printStackTrace();
		}
		RequestDispatcher rd = request.getRequestDispatcher("/principal.jsp");
		rd.forward(request, response);
	}

}



